﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class LoanInfo : System.Web.UI.Page
{
    private readonly string mCurrentModule = Credits.Module.ID;
    private readonly string mPageName = "Vizualizarea scurta a contractelor";
    private string appPath = string.Empty;

    bool allowLoanDataView = false;
    bool allowLoanDataEdit = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        appPath = Utils.GetApplicationPath(Request);
        Utils.GetMaster(this).PerformPreloadActions(mCurrentModule, mPageName);

        allowLoanDataView = Utils.PermissionAllowed(mCurrentModule, Credits.Domains.ManageLoans.Name, Constants.Constants.Classifiers.Permissions_View);
        allowLoanDataEdit = Utils.PermissionAllowed(mCurrentModule, Credits.Domains.ManageLoans.Name, Constants.Constants.Classifiers.Permissions_Edit);

        if (allowLoanDataView)
        {
            if (!IsPostBack)
            {
                if (Request["cd"] != null && !Request["cd"].ToString().Equals(string.Empty))
                {
                    string creditID = Request["cd"].ToString();
                    int loanID = 0;
                    int.TryParse(creditID, out loanID);

                    if (Utils.GetMaster(this.Page).IsEmptyNavLinks)
                    {
                        Utils.GetMaster(this).AddNavlink("Clients", "../ModuleCredits/Clients.aspx", "ClientsPageID");
                    }

                    Credits.DataObjects.LoanObject loanObject = Utils.ModuleCredits().GetLoanObjectByID(loanID);
                    Utils.GetMaster(this).AddNavlink("Codul contractului:" + (loanObject != null ? loanObject.ContractCode.ToString() : string.Empty), "../ModuleCredits/LoanInfo.aspx?cd=" + creditID, "LoanInfoPageID");

                    FillFirstInfoForm(loanObject);
                    FillRepaymentSchedule(loanID);
                    FillAccountDetail(loanID);
                }           
            }
        }
    }

    public DateTime fTranzactionDate = new DateTime();
    public decimal fTranzactionAmount = 0;
    #region principal
    private decimal principalOriginalSummary = 0;
    private decimal principalPaidSummary = 0;
    private decimal principalWaivedSummary = 0;
    private decimal principalWrittenOffSummary = 0;
    private decimal principalOutstandingSummary = 0;
    private decimal principalOverDueSummary = 0;
    #endregion
    #region interest
    private decimal interestOriginalSummary = 0;
    private decimal interestPaidSummary = 0;
    private decimal interestWaivedSummary = 0;
    private decimal interestWrittenOffSummary = 0;
    private decimal interestOutstandingSummary = 0;
    private decimal interestOverDueSummary = 0;
    #endregion
    #region Fees
    private decimal feesOriginalSummary = 0;
    private decimal feesPaidSummary = 0;
    private decimal feesWaivedSummary = 0;
    private decimal feesWrittenOffSummary = 0;
    private decimal feesOutstandingSummary = 0;
    private decimal feesOverDueSummary = 0;
    #endregion
    #region Penalties
    private decimal penaltiesOriginalSummary = 0;
    private decimal penaltiesPaidSummary = 0;
    private decimal penaltiesWaivedSummary = 0;
    private decimal penaltiesWrittenOffSummary = 0;
    private decimal penaltiesOutstandingSummary = 0;
    private decimal penaltiesOverDueSummary = 0;
    #endregion
    #region Total
    private decimal totalOriginalSummary = 0;
    private decimal totalPaidSummary = 0;
    private decimal totalWaivedSummary = 0;
    private decimal totalWrittenOffSummary = 0;
    private decimal totalOutstandingSummary = 0;
    private decimal totalOverDueSummary = 0;
    #endregion

    private void FillFirstInfoForm(Credits.DataObjects.LoanObject loanObject)
    {
        if (loanObject != null)
        {
            dataEliberariiNumerarLabel.Text = loanObject.LoanStartDate.ToString(Constants.Constants.ISODateBackwardDotsFormat);
            currencyLabel.Text = loanObject.ContractValutaNameInfinitivInWords;
            loanManagerLabel.Text = loanObject.LoanManagerIDString;
            loanIDLabel.Text = loanObject.ContractCode.ToString();
            loanScopeLabel.Text = loanObject.LoanScope;
            loanApruvedAmountLabel.Text = loanObject.ContractAmount.ToString();
            loanDebursedAmountLabel.Text = loanObject.ContractAmount.ToString();
        }
    }

    private void FillRepaymentSchedule(int loanID)
    {
        if (loanID != 0)
        {
            DataTable ordersDT = Utils.ModuleCredits().GetRefundsLoanOrderDT1(loanID);
            refundGridView.DataSource = ordersDT;
            refundGridView.DataBind();
            if (ordersDT != null && ordersDT.Rows.Count > 0)
            {
                int footerID = ordersDT.Rows.Count - 1;
                decimal.TryParse(ordersDT.Rows[footerID]["principalDueID"].ToString(), out principalOriginalSummary);
                decimal.TryParse(ordersDT.Rows[footerID]["mainPays"].ToString(), out principalPaidSummary);
                principalOutstandingSummary = principalOriginalSummary - principalPaidSummary;

                decimal.TryParse(ordersDT.Rows[footerID]["interestID"].ToString(), out interestOriginalSummary);
                decimal.TryParse(ordersDT.Rows[footerID]["profitPays"].ToString(), out interestPaidSummary);
                interestOutstandingSummary = interestOriginalSummary - interestPaidSummary;

                //decimal.TryParse(ordersDT.Rows[footerID][""].ToString(), out feesOriginalSummary);
                decimal.TryParse(ordersDT.Rows[footerID]["feesID"].ToString(), out feesPaidSummary);
                feesOutstandingSummary = feesOriginalSummary - feesPaidSummary;

                decimal.TryParse(ordersDT.Rows[footerID]["penaltiesID"].ToString(), out penaltiesPaidSummary);
                penaltiesOutstandingSummary = penaltiesPaidSummary;
            }
            #region principal
            fSetLabelText(principalOriginalLabel, principalOriginalSummary);
            fSetLabelText(principalPaidLabel, principalPaidSummary);
            fSetLabelText(principalWaivedLabel, principalWaivedSummary);
            fSetLabelText(principalWrittenOffLabel, principalWrittenOffSummary);
            fSetLabelText(principalOutstandingLabel, principalOutstandingSummary);
            fSetLabelText(principalOverDueLabel, principalOverDueSummary);
            #endregion
            #region interest
            fSetLabelText(interestOriginalLabel, interestOriginalSummary);
            fSetLabelText(interestPaidLabel, interestPaidSummary);
            fSetLabelText(interestWaivedLabel, interestWaivedSummary);
            fSetLabelText(interestWrittenOffLabel, interestWrittenOffSummary);
            fSetLabelText(interestOutstandingLabel, interestOutstandingSummary);
            fSetLabelText(interestOverDueLabel, interestOverDueSummary);
            #endregion
            #region Fees
            fSetLabelText(feesOriginalLabel, feesOriginalSummary);
            fSetLabelText(feesPaidLabel, feesPaidSummary);
            fSetLabelText(feesPaidLabel, feesPaidSummary);
            fSetLabelText(feesWaivedLabel, feesWaivedSummary);
            fSetLabelText(feesWrittenOffLabel, feesWrittenOffSummary);
            fSetLabelText(feesOutstandingLabel, feesOutstandingSummary);
            fSetLabelText(feesOverDueLabel, feesOverDueSummary);
            #endregion
            #region Penalties
            fSetLabelText(penaltiesOriginalLabel, penaltiesOriginalSummary);
            fSetLabelText(penaltiesPaidLabel, penaltiesPaidSummary);
            fSetLabelText(penaltiesWaivedLabel, penaltiesWaivedSummary);
            fSetLabelText(penaltiesWrittenOffLabel, penaltiesWrittenOffSummary);
            fSetLabelText(penaltiesOutstandingLabel, penaltiesOutstandingSummary);
            fSetLabelText(penaltiesOverDueLabel, penaltiesOverDueSummary);
            #endregion
            #region Total
            totalOriginalSummary = principalOriginalSummary + interestOriginalSummary + feesOriginalSummary + penaltiesOriginalSummary;
            fSetLabelText(totalOriginalLabel, totalOriginalSummary);

            totalPaidSummary = principalPaidSummary + interestPaidSummary + feesPaidSummary + penaltiesPaidSummary;
            fSetLabelText(totalPaidLabel, totalPaidSummary);

            totalWaivedSummary = principalWaivedSummary + interestWaivedSummary + feesWaivedSummary + penaltiesWaivedSummary;
            fSetLabelText(totalWaivedLabel, totalWaivedSummary);

            totalWrittenOffSummary = principalWrittenOffSummary + interestWrittenOffSummary + feesWrittenOffSummary + penaltiesWrittenOffSummary;
            fSetLabelText(totalWrittenOffLabel, totalWrittenOffSummary);

            totalOutstandingSummary = principalOutstandingSummary + interestOutstandingSummary + feesOutstandingSummary + penaltiesOutstandingSummary;
            fSetLabelText(totalOutstandingLabel, totalOutstandingSummary);

            totalOverDueSummary = principalOverDueSummary + interestOverDueSummary + feesOverDueSummary + penaltiesOverDueSummary;
            fSetLabelText(totalOverDueLabel, totalOverDueSummary);
            #endregion
        }
    }

    private void fSetLabelText(Label resultLabel, decimal decimalValue)
    {
        resultLabel.Text = string.Format("{0:0.00}", decimalValue);
    }

    private void FillAccountDetail(int loanID)
    {
        DataTable sourceDT = new DataTable();
        try
        {
            sourceDT = Utils.ModuleMain().GetAccountDetail(loanID);
            if (sourceDT != null && sourceDT.Rows.Count == 1)
            {
                repaymentStrategyLabel.Text = sourceDT.Rows[0]["Repayment Strategy"].ToString();
                repaymentsLabel.Text = sourceDT.Rows[0]["Repayments"].ToString();
                amortizationLabel.Text = sourceDT.Rows[0]["Amortization"].ToString();
                interestLabel.Text = sourceDT.Rows[0]["Amortization"].ToString();
                gracePrincipalLabel.Text = sourceDT.Rows[0]["Grace Principal Payment"].ToString();
                graceInterestLabel.Text = sourceDT.Rows[0]["Grace Interest Payment"].ToString();
                graceArrearsLabel.Text = sourceDT.Rows[0]["Grace Arrears Ageing"].ToString();
                fundSourceLabel.Text = sourceDT.Rows[0]["Fund Source"].ToString();
                interestFreePeriodLabel.Text = sourceDT.Rows[0]["Interest Free Period"].ToString();
                interestCalculationPeriodLabel.Text = sourceDT.Rows[0]["Interest Calculation Period"].ToString();
                interestTypeLabel.Text = sourceDT.Rows[0]["Interest Type"].ToString();
                submittedOnLabel.Text = sourceDT.Rows[0]["Submitted"].ToString();
                approvedOnLabel.Text = sourceDT.Rows[0]["Approved"].ToString();
                disbursedOnLabel.Text = sourceDT.Rows[0]["Disbursed"].ToString();
                matureseOnLabel.Text = sourceDT.Rows[0]["Matures"].ToString();
            }
        }
        catch (Exception ex)
        {
            //todo
        }
    }    
}